//
//  GuideViewController.m
//  wsgz
//
//  Created by huck on 2017/5/13.
//  Copyright © 2017年 BoingPay. All rights reserved.
//

#import "GuideViewController.h"
#import "AppUtils.h"
#import "Config.h"
#import "GuideCollectionViewCell.h"
#import "AppDelegate.h"

#define kGuidePageCount 3
static NSString *const GuideViewCellIdentifier = @"GuideViewCellIdentifier";

@interface GuideViewController()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic) BOOL firstLoad;

@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubview];
}

-(void)initSubview
{
    [_guideCollectionView registerClass:[GuideCollectionViewCell class] forCellWithReuseIdentifier:GuideViewCellIdentifier];
    [_pageControl setNumberOfPages:kGuidePageCount];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [Config saveMyHabitUpdateVersion:[AppUtils currentVersion] ownerUserId:NEED_NEW_GUIDE? [AppUtils currentVersion]  :nil configType:ConfigGuideViewFollowTime];
}


#pragma mark --UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark --UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return kGuidePageCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GuideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GuideViewCellIdentifier forIndexPath:indexPath];
    cell.rowIndex = indexPath.section;
    [cell setContent];
    return cell;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

//这个代理方法保证第一张页面的动画在加载后能执行,如果第一张不用动画,这个就不用了
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0)
{
    if(indexPath.section == 0 && !_firstLoad){
        GuideCollectionViewCell *cell0  = (GuideCollectionViewCell *)cell;
        [cell0 beginAnimation];
        _firstLoad = YES;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint pInView = [self.view convertPoint:_guideCollectionView.center toView:_guideCollectionView];
    NSIndexPath *indexPathNow = [_guideCollectionView indexPathForItemAtPoint:pInView];
    GuideCollectionViewCell *cell  = (GuideCollectionViewCell *)[_guideCollectionView cellForItemAtIndexPath:indexPathNow];
    [cell beginAnimation];
    
    _pageControl.currentPage = indexPathNow.section;
}

//隐藏引导页面
-(void)hideGuideView
{
    CATransition *animation = [CATransition animation];
    animation.duration = 1.2f;
    animation.type = @"rippleEffect";
    [self.view.window.layer addAnimation:animation forKey:nil];
    
    [[AppUtils getAppDelegate] initMainWindow];
}

@end
